package com.atung.merchant.config.auth;

import com.atung.merchant.dto.AuthUserEntity;
import com.atung.merchant.enums.RoleType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @author Administrator
 */
@Component
public class AuthUserMangerContext {

    public static Long getUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        AuthUserEntity authUserEntity = (AuthUserEntity) (authentication.getPrincipal());
        return authUserEntity.getId();
    }

    public static Long getMerchantId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        AuthUserEntity authUserEntity = (AuthUserEntity) (authentication.getPrincipal());
        return authUserEntity.getMerchantId();
    }
    public static RoleType getUserRole(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        AuthUserEntity authUserEntity = (AuthUserEntity) (authentication.getPrincipal());
        return authUserEntity.getType().equals(RoleType.MASTER)?RoleType.MASTER:RoleType.USER;
    }

    public static AuthUserEntity getAuthUserEntity() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        AuthUserEntity authUserEntity = (AuthUserEntity) (authentication.getPrincipal());
        return authUserEntity;
    }
}
